<?php

/**
 *
 * @author Administrator
 */
class ProductController {
    // TODO - Insert your code here
    private $path;
    private $lan;
    
    /**
     */
    function __construct() {
        
        $this->path = "../src/module/admin/view_products/temp/";
        $this->hotArray = array(1 => "Sản phẩm nổi bật", 0 => "Không phải sản phẩm nổi bật");
        $this->feshArray = array(1 => "Sản phẩm mới nhập kho", 0 => "Không phải sản phẩm mới");

        if (isset( $_SESSION[SESSION_LANGUAGE])) {
        	$this->lan = $_SESSION[SESSION_LANGUAGE];
        } else {
        	$this->lan = DEFAULT_LANGAUGE;
        }
    }
    
    /**
     * view table page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewTable(){
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/table.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_PRODUCTS);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_DELETE_MANY);
        
        
        // get array cate product
        $carCtrl = new CategoryController( "");
        $cateArray = $carCtrl->adminCateArray( 'product');
        $cateArray['no'] = "--- Không có danh mục ----";
        
        // select list
        $db = new DBAccess();
        if (isset($_REQUEST['CategoryID'])){
            $selected = $_REQUEST['CategoryID'];
            if ($selected == ""){
                $list = $db->selectManyByCauseWithPaging( $GLOBALS['productShortColArray'], PRODUCTS, array( PRODUCT_LANGUAGE => $this->lan), array(PRODUCT_ID => DESC), 1, 10000);
            } else if ($selected == "no"){
                $conArrayVaule = array();
                $conArrayVaule = createSubIDArray(0, $conArrayVaule);
                $conArray = array(array('key' => NOT_IN, 'column' => PRODUCT_CATEGORYID, 'value' => $conArrayVaule), SQL_AND, array('key' => LIKE, 'column' => PRODUCT_LANGUAGE, 'value' => $this->lan));
                $list = $db->selectDifficultRecord( $GLOBALS['productShortColArray'], PRODUCTS, $conArray, array(PRODUCT_ID => DESC), 1, 10000);
            } else {
                $list = $db->selectDifficultRecord( $GLOBALS['productShortColArray'], PRODUCTS, createSubConditionByID($selected, PRODUCT_CATEGORYID, $this->lan), array(PRODUCT_ID => DESC), 1, 10000);
            }
        } else {
            $selected = "";
            $list = $db->selectManyByCauseWithPaging( $GLOBALS['productShortColArray'], PRODUCTS, array( PRODUCT_LANGUAGE => $this->lan), array(PRODUCT_ID => DESC), 1, 10000);
        }
        
        // assign item
        foreach ( $list as $item ) {
            $cate = $db->selectOneByCause(array(CATEGORY_TITLE), CATEGORIES, array(CATEGORY_ID => $item[PRODUCT_CATEGORYID]));
            $item[PRODUCT_CATEGORYID] = $cate[CATEGORY_TITLE];
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT . "." . ITEM);
        }
        
        $selectCate = createSelectTagFormData( PRODUCT_CATEGORYID, "abc", $cateArray, $selected, "this.form.submit()");
        $template->assign( 'SelectCate', $selectCate);
        
        // assign total count and total page
        $count = $db->selectCountRecord( PRODUCTS, array(PRODUCT_LANGUAGE => $this->lan));
        $template->assign( TOTALCOUNT, $count);
        $template->assign( TOTALPAGE, 1);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }
    
    /**
     * view form page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewForm(){
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/form.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_PRODUCTS);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
        
        // get id
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
            $db = new DBAccess();
            $item = $db->selectOneByCause( $GLOBALS['productFullColArray'], PRODUCTS, array(PRODUCT_ID => $_REQUEST[PARAM_ID]));
            $title = $GLOBALS['formTitleArray'][PRODUCTS][EDIT][$this->lan] . $item[PRODUCT_TITLE];
        } else {
            $item = array();
			$item[PRODUCT_ID] = 0;
			$item[PRODUCT_TITLE] = "";
			$item[PRODUCT_PRODUCTCODE] = "";
			$item[PRODUCT_MANUFACTURER] = "";
			$item[PRODUCT_STATUS] = "";
			$item[PRODUCT_WARRANTY] = "";
			$item[PRODUCT_QUANTITY] = 1;
			$item[PRODUCT_IMAGELINK] = "";
			$item[PRODUCT_CATEGORYID] = "";
			$item[PRODUCT_PRICE] = 0;
			$item[PRODUCT_CURRENCY] = "VND";
			$item[PRODUCT_SHOWPRICE] = 0;
			$item[PRODUCT_SHOWCURRENCY] = "VND";
			$item[PRODUCT_PROMOTION] = "";
			$item[PRODUCT_SPACEPRODUCT] = 0;
			$item[PRODUCT_SPECIFICATION] = "";
			$item[PRODUCT_CONTENT] = "";
			$item[PRODUCT_HOT] = 0;
			$item[PRODUCT_FESH] = 0;
			$item[PRODUCT_VIEWNUMBER] = "";
			$item[PRODUCT_METADESCRIPTION] = "";
			$item[PRODUCT_METAKEYWORD] = "";
			$item[PRODUCT_TAGKEYWORD] = "";
            $title = $GLOBALS['formTitleArray'][PRODUCTS][ADD][$this->lan];
        }
        
        $item[PRODUCT_PRICE] = addComma( $item[PRODUCT_PRICE]);
        $item[PRODUCT_SHOWPRICE] = addComma( $item[PRODUCT_SHOWPRICE]);
        $item[PRODUCT_HOT] = createSelectTagFormData( PRODUCT_HOT, "full-width", $this->hotArray, $item[PRODUCT_HOT]);
        $item[PRODUCT_FESH] = createSelectTagFormData( PRODUCT_FESH, "full-width", $this->feshArray, $item[PRODUCT_FESH]);
        $item[PRODUCT_CURRENCY] = createSelectTagFormData( PRODUCT_CURRENCY, "full-width", $GLOBALS['currency'], $item[PRODUCT_CURRENCY]);
        $item[PRODUCT_SHOWCURRENCY] = createSelectTagFormData( PRODUCT_SHOWCURRENCY, "full-width", $GLOBALS['currency'], $item[PRODUCT_SHOWCURRENCY]);
        
        $carCtrl = new CategoryController( "");
        $cateArray = $carCtrl->adminCateArray( CATEGORY_TYPE_PRODUCT);
        $item[PRODUCT_CATEGORYID] = createSelectTagFormData( PRODUCT_CATEGORYID, "full-width", $cateArray, $item[PRODUCT_CATEGORYID]);
        
        // assign gird title
        $template->assign( FORMTITLE, $title);
        
        // assign form item
        $template->assign( ITEM, $item);
        $template->parse( PAGECONTENT . "." . ITEM);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }
    
    /**
     * 
     * @return multitype:string
     */
    public function submitForm(){
        
        // reponse array
        $response_array = array();
        
        // get input
        $item = array();
        
        $item[PRODUCT_ID] = $_REQUEST[CATEGORY_ID];
		$item[PRODUCT_TITLE] = $_REQUEST[PRODUCT_TITLE];
		
		$item[PRODUCT_PRODUCTCODE] = $_REQUEST[PRODUCT_PRODUCTCODE];
		$item[PRODUCT_MANUFACTURER] = $_REQUEST[PRODUCT_MANUFACTURER];
		$item[PRODUCT_MANUFACTURERCODE] = encodeURL( $_REQUEST[PRODUCT_MANUFACTURER]);
		
		$item[PRODUCT_SIZE] = $_REQUEST[PRODUCT_SIZE];
		$item[PRODUCT_COLOR] = $_REQUEST[PRODUCT_COLOR];
		$item[PRODUCT_COLORCODE] = encodeURL( $_REQUEST[PRODUCT_COLOR]);
		
		$item[PRODUCT_STATUS] = $_REQUEST[PRODUCT_STATUS];
		$item[PRODUCT_WARRANTY] = $_REQUEST[PRODUCT_WARRANTY];
		$item[PRODUCT_QUANTITY] = $_REQUEST[PRODUCT_QUANTITY];
		
		$item[PRODUCT_IMAGELINK] = $_REQUEST[PRODUCT_IMAGELINK];
		
		$item[PRODUCT_CATEGORYID] = $_REQUEST[PRODUCT_CATEGORYID];
		
		$item[PRODUCT_PRICE] = editEntryPrice( $_REQUEST[PRODUCT_PRICE]);
		$item[PRODUCT_CURRENCY] = $_REQUEST[PRODUCT_CURRENCY];
		$item[PRODUCT_SHOWPRICE] = editEntryPrice( $_REQUEST[PRODUCT_SHOWPRICE]);
		$item[PRODUCT_SHOWCURRENCY] = $_REQUEST[PRODUCT_SHOWCURRENCY];
		
		$item[PRODUCT_PROMOTION] = $_REQUEST[PRODUCT_PROMOTION];
		
		$item[PRODUCT_HOTFEATURE] = $_REQUEST[PRODUCT_HOTFEATURE];
		$item[PRODUCT_CONTENT] = $_REQUEST[PRODUCT_CONTENT];
		$item[PRODUCT_SPECIFICATION] = $_REQUEST[PRODUCT_SPECIFICATION];
		
		$item[PRODUCT_HOT] = $_REQUEST[PRODUCT_HOT];
		$item[PRODUCT_FESH] = $_REQUEST[PRODUCT_HOT];
		
        $item[PRODUCT_TITLETAG] = $_REQUEST[ARTICLE_TITLETAG];
        $item[PRODUCT_URL] = encodeURL( $_REQUEST[ARTICLE_URL]);
        $item[PRODUCT_METAKEYWORD] = $_REQUEST[ARTICLE_METAKEYWORD];
        $item[PRODUCT_METADESCRIPTION] = $_REQUEST[ARTICLE_METADESCRIPTION];
        
        $item[PRODUCT_UPDATETIME] = getCurrentDateTime();
        $item[PRODUCT_LANGUAGE] = $this->lan;
        $item[PRODUCT_EDITBY] = $_SESSION[SESSION_USERNAME];
        
        if (empty($item[PRODUCT_TITLETAG])){
            $item[PRODUCT_TITLETAG] = $item[PRODUCT_TITLE];
        }
        if (empty($item[ARTICLE_URL])){
            $item[PRODUCT_URL] = encodeURL( $_REQUEST[PRODUCT_TITLE]);
        }
        
        // check url
        if (checkURLExist( PRODUCTS, $item[PRODUCT_URL], $item[PRODUCT_ID])) {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = getMessage( MESSAGE_URL_EXISTED);
        
            // return repnose array;
            return $response_array;
        }
        
        // update data to database
        $db = new DBAccess();
        
        if ($item[ARTICLE_ID] == 0) {
            if ($db->insertData( $item, PRODUCTS)) {
                $item = $db->selectOneByCause( array(PRODUCT_ID, PRODUCT_URL), PRODUCTS, array(PRODUCT_URL => $item[PRODUCT_URL]));
                updateDataType(PRODUCTS, $item[PRODUCT_ID], $item[PRODUCT_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_PRODUCTS . "&act=" . PVALUE_ACTION_VIEW_TABLE;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
            }
        } else {
            if ($db->updateData( $item, PRODUCTS)) {
                updateDataType(PRODUCTS, $item[PRODUCT_ID], $item[PRODUCT_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_PRODUCTS . "&act=" . PVALUE_ACTION_VIEW_TABLE;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
            }
        }
        
        
        // return repnose array;
        return $response_array;
        
    }
    
    /**
     * submit delete one function
     * @return multitype:
     */
    public function submitDeleteOne(){
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
        
            $db = new DBAccess();
        
            if ($db->deleteData( PRODUCTS, $_REQUEST[PARAM_ID])) {
                deleteDataType(PRODUCTS, $_REQUEST[PARAM_ID]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
            }
        } else {
            $response_array[STATUS] = STATUS_FAIL;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }
    
    /**
     * submit delete many function
     * @return multitype:
     */
    public function submitDeleteMany(){
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_SELECTED])) {
        
            $IDArray = $_REQUEST[PARAM_SELECTED];
            
            if ($IDArray != null && count( $IDArray) > 0) {
                $delete = true;
                $db = new DBAccess();
                foreach ( $IDArray as $id ) {
                    if ($db->deleteData( PRODUCTS, $id)) {
                        deleteDataType(PRODUCTS, $id);
                    } else {
                        $delete = false;
                    }
                }
        
                if ($delete) {
                    $response_array[STATUS] = STATUS_OK;
                    $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
                } else {
                    $response_array[STATUS] = STATUS_FAIL;
                    $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
                }
            } else {
                $response_array[STATUS] = STATUS_ERROR;
                $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
            }
        } else {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }

    /**
     */
    function __destruct() {
        
        // TODO - Insert your code here
    }
}

?>